package club.zhangliyuanblog.controller;

import club.zhangliyuanblog.service.PaymentHystrixService;
import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @author liyuan.zhang
 * @date 2021/4/23 18:54
 */
@Slf4j
@RestController
@RequestMapping("/consumer/order")
@DefaultProperties(defaultFallback = "orderTimeoutHandleGlobal")
public class OrderController {

    @Resource
    private PaymentHystrixService paymentHystrixService;

    @GetMapping(value = "/ok/{id}")
    public String paymentOk(@PathVariable("id") Integer id) {
        return paymentHystrixService.paymentOk(id);
    }

    @GetMapping(value = "/timeout/{time}")
//    @HystrixCommand(fallbackMethod = "orderTimeoutHandle", commandProperties = {
//            @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "3000")
//    })
    @HystrixCommand
    public String timeout(@PathVariable Integer time){
        return paymentHystrixService.timeout(time);
    }

    public String orderTimeoutHandle(Integer time) {
        return "我是消费者80，对方支付系统繁忙，请10秒钟之后再试，┭┮﹏┭┮";
    }

    public String orderTimeoutHandleGlobal(){
        return "全局的异常处理信息，请稍后再试┭┮﹏┭┮";
    }

}
